<?php
require_once "includes/db.php";
require_once "includes/functions.inc.php"; 

if (RetrieveUserIDFromHash() < 1)
    die("user didn't exist");
?>
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- xmlns="http://www.w3.org/1999/xhtml" //-->
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>BobShaker</title>
        <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAeEKd2fOlzODVgs2cXmBmFhRskU71bb7iuLb63i6s7fl11-3ghBQ3xTUfx2H3h1bQ--Q0C0K7q4GA5g&sensor=false"
        type="text/javascript"></script>
        <script type="text/javascript" src="eshapes.js"></script>
    </head>
    <body onload="initialize()" onunload="GUnload()">
        <?php
        $sql = "select users.user_id, user_first_name, user_last_name, x, y, user_last_update, bob from users, friends where friends.friend_user_id_1 = '" . $split[0] . "' and friends.friend_user_id_2 = users.user_id and user_last_update > '" . date("Y-m-d H:i:s", time() - 21600) . "'";
        $result = mysql_query($sql);

        if (!$result) 
        {
            echo("<!-- SQL ERROR: Error performing query: " . mysql_error() . "//-->");
        }

        $num_rows = mysql_num_rows($result);

        if ($num_rows == 0) 
        {
            echo "Geen posities gevonden.";
        } 
        else 
        { ?>
            <div id="map_canvas" style="width: 100%; height: 100%"></div>

            <?php
            $output = "<script type=\"text/javascript\">\n\nfunction initialize() {\n";
            $output .= "if (GBrowserIsCompatible()) {\n";
            $output .= "var map = new GMap2(document.getElementById(\"map_canvas\"));\n";
            $output .= "map.setMapType(G_HYBRID_MAP);\n";

            $query = "SELECT x, y FROM users WHERE user_id = '" . $split[0] . "';";
            $rezult = mysql_query($query);

            if (mysql_num_rows($rezult) == 1) 
            {
                $row = mysql_fetch_array($rezult);
                $output .= "map.setCenter(new GLatLng(" . $row['x'] . ", " . $row['y'] . "), 16);\n";
                $hetBericht = "You are here!";
                $jijzelf = "map.addOverlay(createMarker(new GLatLng(" . $row['x'] . "," . $row['y'] . "),'" . $hetBericht . "'));\n";
            }

            $output .= "map.addControl(new GSmallMapControl());\n";
            $output .= "map.addControl(new GMapTypeControl());\n\n";

            $output .="function createMarker(point, message) {\n";
            $output .="var marker = new GMarker(point);\n";
            $output .="GEvent.addListener(marker, \"click\", function() {\n";
            $output .="var myHtml = message;\n";
            $output .="map.openInfoWindowHtml(point, myHtml);\n";
            $output .="});\n";
            $output .="return marker;\n";
            $output .="}\n";

            echo $output;

            echo $jijzelf;

            while ($rij = mysql_fetch_array($result)) 
            {
                $hetBericht = $rij['user_first_name'] . " " . strtoupper(substr($rij['user_first_name'], 0, 1)) . ". was hier op: " . $rij['user_last_update'];
                echo "map.addOverlay(createMarker(new GLatLng(" . $rij['x'] . "," . $rij['y'] . "),'" . $hetBericht . "'));\n";
            }
            echo "\n}\n}\n\n</script>\n";
        } ?>
    </body>
</html>